package com.wang.sort;

import java.util.Stack;
//获取栈中的最小值 时间复杂度O(1)
public class Demo13_GetMinStack {
    public class MyStack {
        private Stack<Integer> stackData;//数据栈
        private Stack<Integer> stackMin;//辅助栈
        public MyStack() {
            this.stackData = new Stack<Integer>();
            this.stackMin = new Stack<Integer>();
        }
        public void push(int newNum) {
            if(this.stackMin.isEmpty()){
                this.stackMin.push(newNum);
            }
            else if(newNum < this.getMin()){
                this.stackMin.push(newNum);
            }
            else {
                int newMin =this.stackMin.peek();
                this.stackMin.push(newMin);
            }
            this.stackData.push(newNum);
        }

        public int pop() {
            if (this.stackData.isEmpty()) {
                throw new RuntimeException("Your stack is empty!");
            }
            this.stackMin.pop();
            return this.stackData.pop();
        }

        private int getMin() {
            if(this.stackMin.isEmpty()){
                throw new RuntimeException("Your stack is empty");
            }
            return this.stackMin.peek();
        }
    }

}
